<?php
/**
 * Created by PhpStorm.
 * User: wzy12
 * Date: 2018/10/17
 * Time: 15:16
 */

namespace app\admin\controller;


class Orders extends Base
{
    public function order(){
        $search_key = input();
        unset($search_key['/admin/orders/order_html']);

        $list = model('Order') -> resList($search_key);
        unset($search_key['page']);
        if(!$search_key){
            $search_key = null;
        }

        $this -> assign('search_key', $search_key);
        $this -> assign('list', $list);
        return $this -> fetch();
    }

    /**
     * 查看订单
     */
    public function order_cat(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = model('OrderAction') -> operation($data);
            if(!$result['code']){
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }else{
                return $this -> success($result['msg'], url('orders/order_cat',['id' => $data['order_id']]), ['token' => $this -> request -> token()]);
            }
        }else{
            $id = input('id', 0);
            if($id){
                $order = model('Order') -> Cat($id);

                $this -> assign('order', $order);
                return $this -> fetch();
            }else{
                return $this -> error('非法请求', url('index/index'));
            }
        }
    }

    /**
     * 修改订单
     */
    public function order_form(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = model('Order') -> operation($data);
            if(!$result['code']){
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }else{
                return $this -> success($result['msg'], url('orders/order_cat',['id' => $data['id']]), ['token' => $this -> request -> token()]);
            }
        }else{
            $id = input('id', 0);
            if($id){
                $order = model('Order') -> Cat($id);

                $this -> assign('order', $order);
                return $this -> fetch();
            }else{
                return $this -> error('非法请求', url('index/index'));
            }
        }
    }

    /**
     * 删除订单
     */
    public function order_del(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $res = model('Order') -> del($data);
            if($res['code']){
                return $this -> success($res['msg'], url('orders/ordre'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($res['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            return $this -> error('非法请求', url('index/index'));
        }
    }

    /**
     * 修改费用
     * @return mixed|void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function order_cost(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = model('Order') -> editCost($data);
            if(!$result['code']){
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }else{
                return $this -> success($result['msg'], url('orders/order_cat',['id' => $data['id']]), ['token' => $this -> request -> token()]);
            }
        }else{
            $id = input('id', 0);
            if($id){
                $order = model('Order') -> field('id,goods_price,shipping_price,discount,total_amount,order_amount') -> find($id);

                $this -> assign('order', $order);
                return $this -> fetch();
            }else{
                return $this -> error('非法请求', url('index/index'));
            }
        }
    }

    /**
     * 订单发货
     */
    public function order_deliver(){
        if($this -> request -> isAjax()){
            $data = input('post.');
            $result = model('Delivery') -> operation($data);

            if(!$result['code']){
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }else{
                return $this -> success($result['msg'], url('orders/order_cat',['id' => $data['order_id']]), ['token' => $this -> request -> token()]);
            }
        }else{
            $id = input('id', 0);
            if($id){
                $order = model('Order') -> Cat($id);

                $this -> assign('order', $order);
                return $this -> fetch();
            }else{
                return $this -> error('非法请求', url('index/index'));
            }
        }
    }

    /**
     * 发货单
     */
    public function orderGoodsList(){
        $search_key = input();
        unset($search_key['/admin/orders/ordergoodslist_html']);

        $list = model('Order') -> deliver_goods($search_key);
        unset($search_key['page']);
        if(!$search_key){
            $search_key = null;
        }

        $this -> assign('search_key', $search_key);
        $this -> assign('list', $list);

        return $this -> fetch();
    }

    /**
     *
     * 退换货表
     */
    public function returnGoods(){
        $search_key = input();
        unset($search_key['/admin/orders/returngoods_html']);

        $list = model('ReturnGoods') -> resList($search_key);
        unset($search_key['page']);
        if(!$search_key){
            $search_key = null;
        }

        $this -> assign('search_key', $search_key);
        $this -> assign('list', $list);

        return $this -> fetch();
    }

    public function return_form(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $result = model('ReturnGoods') -> operation($data);
            if(!$result['code']){
                return $this -> error($result['msg'], '', ['token' => $this -> request -> token()]);
            }else{
                return $this -> success($result['msg'], url('orders/return_form',['id' => $data['id']]), ['token' => $this -> request -> token()]);
            }
        }else{
            $id = input('id', 0);
            if($id){
                $res = model('ReturnGoods') -> resFind($id);

                $this -> assign('id', $id);
                $this -> assign('res', $res);
                return $this -> fetch();
            }else{
                return $this -> error('非法请求', url('index/index'));
            }
        }
    }

    public function return_del(){
        if($this -> request -> isAjax()){
            $data = input('post.');

            $res = model('ReturnGoods') -> del($data);
            if($res['code']){
                return $this -> success($res['msg'], url('ad/ad'), ['token' => $this -> request -> token()]);
            }else{
                return $this -> error($res['msg'], '', ['token' => $this -> request -> token()]);
            }
        }else{
            return $this -> error('非法请求', url('index/index'));
        }
    }
}